$(function () {
    layui.form.verify({
        pwd: [/^[\S]{6,12}$/, '密码必须6到12位，且不能出现空格'],
        diffPwd: function (newPwdStr) {
            const oldPwdStr = $('.layui-form [name=oldPwd]').val();
            if (oldPwdStr == newPwdStr) {
                return '新旧密码不能一样哦'
            }
        },

        repwd(repwdvalue) {
            const pwdStr = $('.layui-form [name=newPwd]').val();
            if (pwdStr !== repwdvalue) return '两次输入不一致，请重新输入!'
        }
    })
    $('.layui-form').on('submit', doSubmit);
})

function doSubmit(e) {
    e.preventDefault();
    let data = layui.form.val('userForm')
    data.reNewPwd = null;
    $.ajax({
        method: 'POST',
        url: '/my/updatepwd',
        data,
        success(res) {
            if (res.status == 1) {
                return layui.layer.alert(res.message, { icon: 2 })
            }
            layui.layer.alert('密码重置成功了哦', function () {
                localStorage.removeItem('token');
                window.parent.location.href = '/login.html';
            })
        }
    })
}